package com.sren.initializrstart.util;

import java.io.Reader;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 * @author sren
 * @create 2018-12-10 上午9:05
 **/

public class XmlBuilder {

    /**
     * 将xml转换为对象
     * @param clazz
     * @param xmlStr
     * @return
     * @throws Exception
     */
    public static Object xml2Obj(Class<?> clazz, String xmlStr) throws Exception {
        Object xmlObj = null;
        Reader reader = null;
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        reader = new StringReader(xmlStr);
        xmlObj = unmarshaller.unmarshal(reader);
        if (reader != null) {
            reader.close();
        }
        return xmlObj;
    }

}
